package Threads.ReentrantReadWriteLock.JUC_other;

import Threads.Utils.log;

import java.util.concurrent.Semaphore;

public class Semahore_test {
    public static void main(String[] args) {
        // 1. 创建 semaphore 对象
        Semaphore semaphore = new Semaphore(3);

        // 2. 10个线程同时运行
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                // 3. 获取许可
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    log.debug("running...");
                    sleep(1);
                    log.debug("end...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                // 4. 释放许可
                    semaphore.release();
                }
            }).start();
        }
    }

    private static void sleep(int i) throws InterruptedException {
        Thread.sleep(i*1000);
    }
}
